const util = require('../../utils/util.js')
const getApps = getApp().globalData
const Monitor = require('../../utils/monitor.js').Monitor

Page(Monitor.hookApp({

  data: {
    waysArr: [],
    lists: [],
    imgUrl: getApps.imgUrl,
    imgUrls: getApps.imgUrls,
    current: 1,
    showSpecification: false,
    timeList: [],
    skuTypeList: [],
    showDistribution: false,
    right: '038b5201805161046447142.png',
    rightImage: "wxshop2854jh5sdf45左边.svg"
  },
  onLoad(options) {
    this.data.limit_id = options.limit_id
    this.data.goods_id = options.goods_id
    this.goodsInfo()

    // this.getShopList()
  },
  onShow() {
    // this.setData({
    //   waysArr: []
    // })
    this.getCardBag()
    this.addBrows(3, this.data.limit_id, this.data.goods_id)
  },
  previewImage(e) {
    var current = e.target.dataset.src;
    var that = this
    wx.previewImage({
      current: that.data.imgUrls + current, // 当前显示图片的http链接
      urls: getimg()
    })

    function getimg() {
      var arrimage = []
      that.data.data.goods_cover.map((item, index) => {
        arrimage.push(that.data.imgUrls + item)
      })
      return arrimage
    }
  },
  showDistribution() {
    this.setData({
      showDistribution: !this.data.showDistribution
    })
  },
  // 被屏了
  jumpDetails(e) {
    wx.redirectTo({
      url: '/pages/shopDetails/shopDetails?goods_id=' + e.currentTarget.dataset.item.goods_id
    })
  },
  // getShopList() {
  //   that = this;
  //   util.API('spwx/v1.Assemble/goodsinfo', {
  //     goods_id: this.data.goods_id,
  //     limit_id: this.data.limit_id
  //   }).then(res => {
  //     let {
  //       code,
  //       data
  //     } = res
  //     if (code == 200) {
  //       that.setData({
  //         lists: res.data.list,
  //       })
  //     }

  //   })
  // },
  addBrows(type, type_id, goods_id) { // 增加商品浏览记录
    util.API('sp/v1.Assemble/addBrows', {
      type, //1普通 2 拼团 3折扣
      type_id, //活动id
      goods_id //活动id
    })
    // this.addBrows(1, 0, this.data.goods_id)
  },
  goodsInfo() {
    var that = this
    var {
      goods_id,
      up_user,
      down_user,
      limit_id
    } = this.data
    util.API('spwx/v1.Assemble/goodsinfo', {
      goods_id,
      limit_id,
      up_user,
      down_user
    }).then(({code, data }) => {
      if (code === 200) {
        data.shop_goods_id = data.goods_id
        data.sku_type = JSON.parse(data.sku_type)
        data.distribution_name = JSON.stringify(data.distribution_name)
        data.limit_id = this.data.limit_id
        data.goods_cover = data.goods_cover.split(",");
        data.goods_content = data.goods_content.replace(/\<img/gi, '<img style="width:100%;height:100%;display:block;margin-top:-1px" ')
        let waysArr = this.setWaysArr(this.data.waysArr, data.distribution_type, data.distribution_name)
        let placeTypes = [];
        data.sku_type.forEach((items, indexs) => {
          placeTypes.push(items.type)
        })
        this.setData({
          distribution_name: JSON.parse(data.distribution_name),
          waysArr,
          placeTypes,
          data
        })
        util.intervalTime(data.activity.etime, this, 1000)
      }
    })
  },
  // 设置自定义配送信息
  setWaysArr(waysArr, distribution_type, distribution_name) {
    distribution_type = distribution_type.split(',');
    distribution_name = JSON.parse(distribution_name)
    let len = distribution_type.length
    for (let i = 0; i < len; i++) {
      waysArr.push(distribution_name[distribution_type[i]])
    }
    return waysArr
  },

  showSpecification() {
    this.setData({
      showSpecification: !this.data.showSpecification
    })
  },


  getCardBag() {
    util.API('spwx/v1.Vipcard/cardBag').then(({
      data
    }) => {
      let {
        noRecieveCards,
        alreadyRecieveCards
      } = data
      this.setData({
        noRecieveCards,
        alreadyRecieveCards
      })
    })
  },
  jumpVip(e) {
    let card_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../Vip/Vip?card_id=${card_id}&index=0&stasus=false`,
    })
  },

  preventTouchMove: function(event) {
    return false
  },

  myevent(e) {
    this.setData({
      showSpecification: e.detail.showSpecification
    })
  },
  onHide: function() {
    clearInterval(this.data.interval);
  },
  onUnload() {
    clearInterval(this.data.interval);
  },

  onReachBottom: function() {

  },
  bindchange(e) {
    this.setData({
      current: e.detail.current + 1
    })
  }
}))